﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppPlanProduccion.Code;
using WebAppPlanProduccion.Code.Security;
using Web.Framework;
using PlanProd.Contracts.Misc;
using PlanProd.Contracts.Search;
using PlanProd.Contracts;

namespace WebAppPlanProduccion.App
{
    public partial class ProductoList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HasPermissionToSeeMe(PlanProdSecurity.PRODUCTO_VER);
            if (!this.IsPostBack)
            {
                UIHelper.LoadTemporadas(this.cboTemporada, "Todas");
                UIHelper.LoadCalidad(this.cboCalidad, "Todas");
            }
        }

        protected override void ApplySecurity()
        {
            this.lnkSincronizar.Visible = HasPermissionFor(PlanProdSecurity.PRODUCTO_SINCRONIZAR);
        }

        protected void lnkBuscar_Click(object sender, EventArgs e)
        {
            try
            {
                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkSincronizar_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    Buscar();
            //}
            //catch (Exception ex)
            //{
            //    ExceptionManager.Instance.HandleException(ex);
            //}
        }        

        protected void GridProductos_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.GridProductos.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }        

        protected void GridProductos_RowCommand(object sender, GridViewCommandEventArgs e)
        {           
        }

        protected void GridProductos_RowDataBound(object sender, GridViewRowEventArgs e)
        {            
        }

        private void Buscar()
        {
            ServiceOptions searchOptions = new ServiceOptions();
            searchOptions.PageNumber = GridProductos.PageIndex;
            searchOptions.PageSize = GridProductos.PageSize;

            SearchProductCriteria productSearchCriteria = new SearchProductCriteria();
            productSearchCriteria.CodigoProducto = this.txtProducto.Text;
            productSearchCriteria.CodigoColor = this.txtColor.Text;
            productSearchCriteria.TemporadaId = UIHelper.GetIntFromInputCbo(cboTemporada);
            productSearchCriteria.CalidadId = UIHelper.GetIntFromInputCbo(cboCalidad);

            List<Producto> productos = WebServiceHelper.SearchProducts(searchOptions, productSearchCriteria);

            this.GridProductos.DataSource = productos;
            this.GridProductos.DataBind();        

            pnlResults.Visible = true;
        }
    }
}